package com.lsw.rabbitmq.service;

import com.lsw.rabbitmq.config.RabbitConfig;
import com.lsw.rabbitmq.entity.LoginMessage;
import com.lsw.rabbitmq.entity.RegistrationMessage;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

@Component
public class MessagingService {
    @Autowired
    RabbitTemplate rabbitTemplate;

    public void sendRegistrationMessage(RegistrationMessage msg) {
        rabbitTemplate.convertAndSend("registration", "", msg);
    }

    public void sendLoginMessage(LoginMessage msg) {
        String routingKey = msg.getSuccess() ? "" : "login_failed";
        rabbitTemplate.convertAndSend("login", routingKey, msg);
    }

    public void sendCreateOrderMessage(String orderId) {
        rabbitTemplate.convertAndSend(  "createOrder", "",orderId);
    }
    public void sendConfirmTestMessage( ) {
        rabbitTemplate.convertAndSend(RabbitConfig.JAVABOY_EXCHANGE_NAME,"RabbitConfig.JAVABOY_QUEUE_NAME","hello rabbitmq!".getBytes(),new CorrelationData(UUID.randomUUID().toString()));
    }
}